package com.zdy.today.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.zdy.widget.CustomVideoView;
import com.zdy.today.R;
import com.zdy.today.base.BaseActivity;

/**
 * Created by jsb-cpyy on 2017/6/15.
 */

public class VideoViewWelcomeActivity extends BaseActivity {
    @Override
    public void initOnCreate(Bundle savedInstanceState) {
        super.initOnCreate(savedInstanceState);
        if( savedInstanceState != null ) {
            stopPosition = savedInstanceState.getInt("position");
        }
    }

    @Override
    protected void init() {
        initVideoView();
    }

    @Override
    protected String setTitle() {
        return "欢迎界面";
    }

    @Override
    protected int getResourceId() {
        return R.layout.activity_video_view_wel;
    }

    public void click(View v) {
        finish();
    }
    private CustomVideoView videoview;
    private int stopPosition;
    private void initVideoView() {
        videoview = (CustomVideoView) findViewById(R.id.videoview);
        //设置播放加载路径
        videoview.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.mqr));
        //播放
        videoview.seekTo(stopPosition);
        //循环播放
        videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoview.start();
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        stopPosition=videoview.getCurrentPosition();
    }

    @Override
    public void onResume() {
        super.onResume();
        videoview.seekTo(stopPosition);
        videoview.start(); //Or use resume() if it doesn't work. I'm not sure
    }
    // This gets called before onPause so pause video here.
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        videoview.pause();
        outState.putInt("position", stopPosition);
    }

    @Override
    protected void onDestroy() {
        videoview.stopPlayback();
        super.onDestroy();
    }
}
